home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP11 / ABOUT3.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.1 KB  |  156 lines

  1. /*------------------------------------------
  2.    ABOUT3.C -- About Box Demo Program No. 3
  3.                (c) Charles Petzold, 1996
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "about3.h"
  8.  
  9. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  10. BOOL    CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
  11. LRESULT CALLBACK EllipPushWndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static char  szAppName[] = "About3" ;
  17.      MSG          msg ;
  18.      HWND         hwnd ;
  19.      WNDCLASSEX   wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = szAppName ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (hInstance, szAppName) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      wndclass.cbSize        = sizeof (wndclass) ;
  37.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  38.      wndclass.lpfnWndProc   = EllipPushWndProc ;
  39.      wndclass.cbClsExtra    = 0 ;
  40.      wndclass.cbWndExtra    = 0 ;
  41.      wndclass.hInstance     = hInstance ;
  42.      wndclass.hIcon         = NULL ;
  43.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  44.      wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
  45.      wndclass.lpszMenuName  = NULL ;
  46.      wndclass.lpszClassName = "EllipPush" ;
  47.      wndclass.hIconSm       = NULL ;
  48.  
  49.      RegisterClassEx (&wndclass) ;
  50.  
  51.      hwnd = CreateWindow (szAppName, "About Box Demo Program",
  52.                           WS_OVERLAPPEDWINDOW,
  53.                           CW_USEDEFAULT, CW_USEDEFAULT,
  54.                           CW_USEDEFAULT, CW_USEDEFAULT,
  55.                           NULL, NULL, hInstance, NULL) ;
  56.  
  57.      ShowWindow (hwnd, iCmdShow) ;
  58.      UpdateWindow (hwnd) ; 
  59.  
  60.      while (GetMessage (&msg, NULL, 0, 0))
  61.           {
  62.           TranslateMessage (&msg) ;
  63.           DispatchMessage (&msg) ;
  64.           }
  65.      return msg.wParam ;
  66.      }
  67.  
  68. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  69.      {
  70.      static HINSTANCE hInstance ;
  71.  
  72.      switch (iMsg)
  73.           {
  74.           case WM_CREATE :
  75.                hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  76.                return 0 ;
  77.  
  78.           case WM_COMMAND :
  79.                switch (LOWORD (wParam))
  80.                     {
  81.                     case IDM_ABOUT :
  82.                          DialogBox (hInstance, "AboutBox", hwnd,
  83.                                     AboutDlgProc) ;
  84.                          return 0 ;
  85.                     }
  86.                break ;
  87.      
  88.           case WM_DESTROY :
  89.                PostQuitMessage (0) ;
  90.                return 0 ;
  91.           }
  92.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  93.      }
  94.  
  95. BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  96.      {
  97.      switch (iMsg)
  98.           {
  99.           case WM_INITDIALOG :
  100.                return TRUE ;
  101.  
  102.           case WM_COMMAND :
  103.                switch (LOWORD (wParam))
  104.                     {
  105.                     case IDOK :
  106.                          EndDialog (hDlg, 0) ;
  107.                          return TRUE ;
  108.                     }
  109.                break ;
  110.           }
  111.      return FALSE ;
  112.      }
  113.  
  114. LRESULT CALLBACK EllipPushWndProc (HWND hwnd, UINT iMsg, WPARAM wParam,
  115.                                                          LPARAM lParam)
  116.      {
  117.      char        szText[40] ;
  118.      HBRUSH      hBrush ;
  119.      HDC         hdc ;
  120.      PAINTSTRUCT ps ;
  121.      RECT        rect ;
  122.  
  123.      switch (iMsg)
  124.           {
  125.           case WM_PAINT :
  126.                GetClientRect (hwnd, &rect) ;
  127.                GetWindowText (hwnd, szText, sizeof (szText)) ;
  128.  
  129.                hdc = BeginPaint (hwnd, &ps) ;
  130.  
  131.                hBrush = CreateSolidBrush (GetSysColor (COLOR_WINDOW)) ;
  132.                hBrush = (HBRUSH) SelectObject (hdc, hBrush) ;
  133.                SetBkColor (hdc, GetSysColor (COLOR_WINDOW)) ;
  134.                SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
  135.  
  136.                Ellipse (hdc, rect.left, rect.top, rect.right, rect.bottom) ;
  137.                DrawText (hdc, szText, -1, &rect,
  138.                               DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  139.  
  140.                DeleteObject (SelectObject (hdc, hBrush)) ;
  141.  
  142.                EndPaint (hwnd, &ps) ;
  143.                return 0 ;
  144.  
  145.           case WM_KEYUP :
  146.                if (wParam != VK_SPACE)
  147.                     break ;
  148.                                         // fall through
  149.           case WM_LBUTTONUP :
  150.                SendMessage (GetParent (hwnd), WM_COMMAND,
  151.                     GetWindowLong (hwnd, GWL_ID), (LPARAM) hwnd) ;
  152.                return 0 ;
  153.           }
  154.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  155.      }
  156.